package GUI;

import ModelLayer.*;
import CtrlLayer.*;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DeliveriesGUI extends JFrame implements ActionListener {
    private Container container;
    private JPanel panelDeliveries;
    private JPanel panelMakeDelivery;
    private JPanel panelShowDelivery;
    private JPanel panelShowDelivery2;
    private JPanel panelDeleteDelivery;
    private JPanel panelDeleteDelivery2;
    JLabel labelProduct;
    JLabel errorMessage;
    JTextField textBarcode;
    JTextField textQuantity;
    private int count;
    private int sessionId;
    private int deliveryId;
    private  DeliveryController deliveryController;

    public DeliveriesGUI(int sessionId) {
        deliveryId = 0;
        this.sessionId = sessionId;
        deliveryController = new DeliveryController();
        panelDeliveries = new JPanel(new GridBagLayout());
        panelMakeDelivery = new JPanel(new GridBagLayout());
        panelShowDelivery = new JPanel(new GridBagLayout());
        panelShowDelivery2 = new JPanel(new GridBagLayout());
        panelDeleteDelivery = new JPanel(new GridBagLayout());
        panelDeleteDelivery2 = new JPanel(new GridBagLayout());
        
        

        this.setTitle("Vestbjerg company system");
        this.setLocation(300,50);
        this.setVisible(true);
        this.setSize(400,470);
        this.setResizable(false);

        makeMenuDeliveries();
    }

    public void actionPerformed(ActionEvent e){
    }

    public void makeDeliveriesMenu() {
        GridBagConstraints gbc = new GridBagConstraints();

        JButton addDelivery = new JButton("Make Delivery");
        addDelivery.setPreferredSize(new Dimension(150,25));
        addDelivery.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    makeDelivery();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveries.add(addDelivery, gbc);

        JButton findDelivery = new JButton("Show Delivery");
        findDelivery.setPreferredSize(new Dimension(150,25));
        findDelivery.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findDelivery();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveries.add(findDelivery, gbc);
        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2){
            JButton deleteDelivery = new JButton("Delete Delivery");
            deleteDelivery.setPreferredSize(new Dimension(150,25));
            deleteDelivery.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        container = getContentPane();
                        container.removeAll();
                        deleteDelivery();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 4;
            gbc.insets = new Insets(5,5,5,5);
            panelDeliveries.add(deleteDelivery, gbc);
        }

        JButton back = new JButton("Close");
        back.setPreferredSize(new Dimension(150, 25));
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    dispose();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelDeliveries.add(back, gbc);
    }

    public void makeMenuDeliveries(){
        makeDeliveriesMenu();
        this.getContentPane().add(panelDeliveries, BorderLayout.NORTH);
    }

    private void menuMakeDelivery(){
        JTextField textCustomerId = new JTextField();
        JTextField textAddress = new JTextField();
        JTextField textPhone = new JTextField();
        JTextField textPrice = new JTextField();
        JLabel labelCustomerId;
        JLabel labelAddress;
        JLabel labelPhone;
        JLabel labelPrice;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JButton buttonAdd;
        JButton buttonRemove;
        JButton buttonSubmit;
        JButton buttonBack;
        JButton buttonClear;
        GridBagConstraints gbc = new GridBagConstraints();

        labelCustomerId = new JLabel("Customer ID:", JLabel.RIGHT);
        labelCustomerId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeDelivery.add(labelCustomerId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerId.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(textCustomerId,gbc);

        labelAddress = new JLabel("Address:", JLabel.RIGHT);
        labelAddress.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeDelivery.add(labelAddress, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textAddress.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(textAddress,gbc);

        labelPhone = new JLabel("Phone:", JLabel.RIGHT);
        labelPhone.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeDelivery.add(labelPhone, gbc);

        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhone.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(textPhone,gbc);

        labelPrice = new JLabel("Price:", JLabel.RIGHT);
        labelPrice.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeDelivery.add(labelPrice, gbc);

        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textPrice.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(textPrice,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeDelivery.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeDelivery.add(labelQuantity, gbc);

        labelProduct = new JLabel("Product 1:", JLabel.RIGHT);
        labelProduct.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeDelivery.add(labelProduct, gbc);

        textBarcode = new JTextField();
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        textBarcode.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(textBarcode,gbc);

        textQuantity = new JTextField();
        gbc.gridx = 2;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        textQuantity.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(textQuantity,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        panelMakeDelivery.add(errorMessage, gbc);

        buttonAdd = new JButton("Add more");
        buttonRemove = new JButton("Remove");
        buttonSubmit = new JButton("Submit");
        buttonBack = new JButton("Back");
        buttonClear = new JButton("Clear");

        buttonAdd.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    if(textBarcode.getText().equals("") || textQuantity.getText().equals("")){
                        errorMessage.setText("Not possible!");
                        buttonAdd.setEnabled(false);
                        buttonRemove.setEnabled(false);
                        buttonSubmit.setEnabled(false);
                        buttonBack.setEnabled(false);
                    }else{

                        if (deliveryId==0)
                            deliveryId = deliveryController.createDelivery(Integer.parseInt(textCustomerId.getText()), textAddress.getText(), 
                                textPhone.getText(), Double.parseDouble(textPrice.getText()), sessionId);
                        String barcode = textBarcode.getText();
                        int productBarcode = Integer.parseInt(barcode);
                        int amount = Integer.parseInt(textQuantity.getText());
                        boolean check = deliveryController.addProductInDelivery(deliveryId,productBarcode,amount);
                        if(!check) {
                            deliveryController.removeProductFromDelivery(deliveryId,productBarcode,amount);
                        }

                        count++;
                        labelProduct = new JLabel("Product " + (count+1) + ":", JLabel.RIGHT);
                        labelProduct.setPreferredSize(new Dimension(100, 25));
                        gbc.gridx = 0;
                        gbc.gridy = 6 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        panelMakeDelivery.add(labelProduct, gbc);

                        textBarcode = new JTextField();
                        gbc.gridx = 1;
                        gbc.gridy = 6 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        textBarcode.setPreferredSize(new Dimension(100,25));
                        panelMakeDelivery.add(textBarcode,gbc);

                        textQuantity = new JTextField();
                        gbc.gridx = 2;
                        gbc.gridy = 6 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        textQuantity.setPreferredSize(new Dimension(100,25));
                        panelMakeDelivery.add(textQuantity,gbc);

                        errorMessage = new JLabel("", JLabel.RIGHT);
                        errorMessage.setPreferredSize(new Dimension(100, 25));
                        gbc.gridx = 0;
                        gbc.gridy = 7 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        panelMakeDelivery.add(errorMessage, gbc);

                        gbc.gridx = 0;
                        gbc.gridy = 8 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonAdd.setPreferredSize(new Dimension(100,25));
                        panelMakeDelivery.add(buttonAdd,gbc);

                        gbc.gridx = 1;
                        gbc.gridy = 8 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonRemove.setPreferredSize(new Dimension(100,25));
                        panelMakeDelivery.add(buttonRemove,gbc);

                        gbc.gridx = 2;
                        gbc.gridy = 8 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonSubmit.setPreferredSize(new Dimension(100,25));
                        panelMakeDelivery.add(buttonSubmit,gbc);

                        gbc.gridx = 1;
                        gbc.gridy = 9 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonClear.setPreferredSize(new Dimension(100,25));
                        panelMakeDelivery.add(buttonClear,gbc);

                        gbc.gridx = 2;
                        gbc.gridy = 9 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonBack.setPreferredSize(new Dimension(100,25));
                        panelMakeDelivery.add(buttonBack,gbc);

                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 8;
        gbc.insets = new Insets(5,5,5,5);
        buttonAdd.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(buttonAdd,gbc);

        buttonRemove.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){

                    //  deliveryController.removeProductFromDelivery(deliveryId,Integer.parseInt(textBarcode),Integer.parseInt(textQuantity));

                    container = getContentPane();
                    panelMakeDelivery.remove(labelProduct);
                    panelMakeDelivery.remove(textBarcode);
                    panelMakeDelivery.remove(textQuantity);
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count--;

                    gbc.gridx = 0;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonAdd.setPreferredSize(new Dimension(100,25));
                    panelMakeDelivery.add(buttonAdd,gbc);

                    gbc.gridx = 1;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonRemove.setPreferredSize(new Dimension(100,25));
                    panelMakeDelivery.add(buttonRemove,gbc);

                    gbc.gridx = 2;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonSubmit.setPreferredSize(new Dimension(100,25));
                    panelMakeDelivery.add(buttonSubmit,gbc);

                    gbc.gridx = 1;
                    gbc.gridy = 6 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonClear.setPreferredSize(new Dimension(100,25));
                    panelMakeDelivery.add(buttonClear,gbc);

                    gbc.gridx = 2;
                    gbc.gridy = 6 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonBack.setPreferredSize(new Dimension(100,25));
                    panelMakeDelivery.add(buttonBack,gbc);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 8;
        gbc.insets = new Insets(5,5,5,5);
        buttonRemove.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(buttonRemove,gbc);

        buttonSubmit.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){

                    if (deliveryId==0)
                        deliveryId = deliveryController.createDelivery(Integer.parseInt(textCustomerId.getText()), textAddress.getText(), 
                            textPhone.getText(), Double.parseDouble(textPrice.getText()), sessionId);
                    String barcode = textBarcode.getText();
                    int productBarcode = Integer.parseInt(barcode);
                    int amount = Integer.parseInt(textQuantity.getText());
                    boolean check = deliveryController.addProductInDelivery(deliveryId,productBarcode,amount);
                    if(!check) {
                        deliveryController.removeProductFromDelivery(deliveryId,productBarcode,amount);
                    }

                    //deliveryController.endDelivery(deliveryId, sessionId);
                    deliveryId=0;
                    count = 0;
                    container = getContentPane();
                    panelMakeDelivery.removeAll();
                    container.removeAll();
                    makeMenuDeliveries();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 8;
        gbc.insets = new Insets(5,5,5,5);
        buttonSubmit.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(buttonSubmit,gbc);

        buttonClear.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textBarcode.setText("");
                    textQuantity.setText("");
                    buttonAdd.setEnabled(true);
                    buttonRemove.setEnabled(true);
                    buttonSubmit.setEnabled(true);
                    buttonBack.setEnabled(true);
                    container = getContentPane();
                    panelMakeDelivery.removeAll();
                    container.removeAll();
                    makeDelivery();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count = 0;
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 9;
        gbc.insets = new Insets(5,5,5,5);
        buttonClear.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(buttonClear,gbc);

        buttonBack.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelMakeDelivery.removeAll();
                    container.removeAll();
                    makeMenuDeliveries();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count = 0;
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 9;
        gbc.insets = new Insets(5,5,5,5);
        buttonBack.setPreferredSize(new Dimension(100,25));
        panelMakeDelivery.add(buttonBack,gbc);
    }

    public void makeDelivery(){
        menuMakeDelivery();
        this.getContentPane().add(panelMakeDelivery, BorderLayout.NORTH);
    }

    private void menuShowDelivery(){
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelShowDelivery.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelShowDelivery.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelShowDelivery.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        findDeliverySecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findDelivery();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No delivery!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelShowDelivery.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textId.setText("");
                    errorMessage.setText("");

                    container = getContentPane();
                    panelShowDelivery.removeAll();
                    container.removeAll();
                    makeMenuDeliveries();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelShowDelivery.add(back,gbc);
    }

    public void findDelivery(){
        menuShowDelivery();
        this.getContentPane().add(panelShowDelivery, BorderLayout.NORTH);
    }

    private void findSecondWindow(int deliveryId){
        JTextField textCustomerId = new JTextField();
        JTextField textAddress = new JTextField();
        JTextField textPhone = new JTextField();
        JTextField textPrice = new JTextField();
        JLabel labelCustomerId;
        JLabel labelAddress;
        JLabel labelPhone;
        JLabel labelPrice;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JButton buttonOk;
        GridBagConstraints gbc = new GridBagConstraints();

        textCustomerId.setEditable(false);
        textAddress.setEditable(false);
        textPhone.setEditable(false);
        textPrice.setEditable(false);

        textCustomerId.setText(Integer.toString(deliveryController.findDelivery(deliveryId).getDeliveryID()));
        textAddress.setText(deliveryController.findDelivery(deliveryId).getCustomerAddress());
        textPhone.setText(deliveryController.findDelivery(deliveryId).getCustomerPhone());
        textPrice.setText(Double.toString(deliveryController.findDelivery(deliveryId).getPrice()));

        labelCustomerId = new JLabel("Customer ID:", JLabel.RIGHT);
        labelCustomerId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelShowDelivery2.add(labelCustomerId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerId.setPreferredSize(new Dimension(100,25));
        panelShowDelivery2.add(textCustomerId,gbc);

        labelAddress = new JLabel("Address:", JLabel.RIGHT);
        labelAddress.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelShowDelivery2.add(labelAddress, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textAddress.setPreferredSize(new Dimension(100,25));
        panelShowDelivery2.add(textAddress,gbc);

        labelPhone = new JLabel("Phone:", JLabel.RIGHT);
        labelPhone.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelShowDelivery2.add(labelPhone, gbc);

        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhone.setPreferredSize(new Dimension(100,25));
        panelShowDelivery2.add(textPhone,gbc);

        labelPrice = new JLabel("Price:", JLabel.RIGHT);
        labelPrice.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelShowDelivery2.add(labelPrice, gbc);

        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textPrice.setPreferredSize(new Dimension(100,25));
        panelShowDelivery2.add(textPrice,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelShowDelivery2.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelShowDelivery2.add(labelQuantity, gbc);

        int y = 6;
        for(int i=0; i<DeliveryContainer.getInstance().findDeliveryById(deliveryId).getProducts().size(); i++){
            int productNumber = i;
            labelProduct = new JLabel("Product "+ ++productNumber +":", JLabel.RIGHT);
            labelProduct.setPreferredSize(new Dimension(100, 25));
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            panelShowDelivery2.add(labelProduct, gbc);

            textBarcode = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textBarcode.setPreferredSize(new Dimension(100,25));
            panelShowDelivery2.add(textBarcode,gbc);

            textQuantity = new JTextField();
            gbc.gridx = 2;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textQuantity.setPreferredSize(new Dimension(100,25));
            panelShowDelivery2.add(textQuantity,gbc);
            y++;

            textBarcode.setEditable(false);
            textQuantity.setEditable(false);
            int key = (int) DeliveryContainer.getInstance().findDeliveryById(deliveryId).getProducts().keySet().toArray()[i];
            textBarcode.setText(Integer.toString(key));
            textQuantity.setText(Integer.toString(deliveryController.findDelivery(deliveryId).getProducts().get(key)));
        }

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelShowDelivery2.add(errorMessage, gbc);

        buttonOk = new JButton("OK");
        buttonOk.addActionListener(new 

            ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findDelivery();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        buttonOk.setPreferredSize(new Dimension(100,25));
        panelShowDelivery2.add(buttonOk,gbc);
    }

    public void findDeliverySecondWindow(int deliveryId){
        findSecondWindow(deliveryId);
        this.getContentPane().add(panelShowDelivery2, BorderLayout.NORTH);
    }

    private void menuDeleteDelivery(){
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteDelivery.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteDelivery.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteDelivery.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        deleteDeliverySecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        
                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        deleteDelivery();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        
                        errorMessage.setText("No delivery!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelDeleteDelivery.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    makeMenuDeliveries();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    
                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelDeleteDelivery.add(back,gbc);
    }

    public void deleteDelivery(){
        menuDeleteDelivery();
        this.getContentPane().add(panelDeleteDelivery, BorderLayout.NORTH);
    }

    private void deleteSecondWindow(int deliveryId){
        JTextField textCustomerId = new JTextField();
        JTextField textAddress = new JTextField();
        JTextField textPhone = new JTextField();
        JTextField textPrice = new JTextField();
        JLabel labelCustomerId;
        JLabel labelAddress;
        JLabel labelPhone;
        JLabel labelPrice;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JButton delete;
        JButton cancel;
        GridBagConstraints gbc = new GridBagConstraints();

        textCustomerId.setEditable(false);
        textAddress.setEditable(false);
        textPhone.setEditable(false);
        textPrice.setEditable(false);

        textCustomerId.setText(Integer.toString(deliveryController.findDelivery(deliveryId).getDeliveryID()));
        textAddress.setText(deliveryController.findDelivery(deliveryId).getCustomerAddress());
        textPhone.setText(deliveryController.findDelivery(deliveryId).getCustomerPhone());
        textPrice.setText(Double.toString(deliveryController.findDelivery(deliveryId).getPrice()));

        labelCustomerId = new JLabel("Customer ID:", JLabel.RIGHT);
        labelCustomerId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteDelivery2.add(labelCustomerId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerId.setPreferredSize(new Dimension(100,25));
        panelDeleteDelivery2.add(textCustomerId,gbc);

        labelAddress = new JLabel("Address:", JLabel.RIGHT);
        labelAddress.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteDelivery2.add(labelAddress, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textAddress.setPreferredSize(new Dimension(100,25));
        panelDeleteDelivery2.add(textAddress,gbc);

        labelPhone = new JLabel("Phone:", JLabel.RIGHT);
        labelPhone.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteDelivery2.add(labelPhone, gbc);

        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhone.setPreferredSize(new Dimension(100,25));
        panelDeleteDelivery2.add(textPhone,gbc);

        labelPrice = new JLabel("Price:", JLabel.RIGHT);
        labelPrice.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteDelivery2.add(labelPrice, gbc);

        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textPrice.setPreferredSize(new Dimension(100,25));
        panelDeleteDelivery2.add(textPrice,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteDelivery2.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteDelivery2.add(labelQuantity, gbc);

        int y = 6;
        for(int i=0; i<DeliveryContainer.getInstance().findDeliveryById(deliveryId).getProducts().size(); i++){
            int productNumber = i;
            labelProduct = new JLabel("Product "+ ++productNumber +":", JLabel.RIGHT);
            labelProduct.setPreferredSize(new Dimension(100, 25));
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            panelDeleteDelivery2.add(labelProduct, gbc);

            textBarcode = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textBarcode.setPreferredSize(new Dimension(100,25));
            panelDeleteDelivery2.add(textBarcode,gbc);

            textQuantity = new JTextField();
            gbc.gridx = 2;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textQuantity.setPreferredSize(new Dimension(100,25));
            panelDeleteDelivery2.add(textQuantity,gbc);
            y++;

            textBarcode.setEditable(false);
            textQuantity.setEditable(false);
            int key = (int) DeliveryContainer.getInstance().findDeliveryById(deliveryId).getProducts().keySet().toArray()[i];
            textBarcode.setText(Integer.toString(key));
            textQuantity.setText(Integer.toString(deliveryController.findDelivery(deliveryId).getProducts().get(key)));
        }

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteDelivery2.add(errorMessage, gbc);

        delete = new JButton("Delete");
        delete.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    deliveryController.deleteDelivery(deliveryId);
                    container = getContentPane();
                    container.removeAll();
                    deleteDelivery();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        delete.setPreferredSize(new Dimension(100,25));
        panelDeleteDelivery2.add(delete,gbc);

        cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    deleteDelivery();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        cancel.setPreferredSize(new Dimension(100,25));
        panelDeleteDelivery2.add(cancel, gbc);

    }

    public void deleteDeliverySecondWindow(int deliveryId){
        deleteSecondWindow(deliveryId);
        this.getContentPane().add(panelDeleteDelivery2, BorderLayout.NORTH);
    }
}